home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 001 / pibt40s1.arc / HEXTODEC.MOD < prev    next >
Text File  |  1986-08-18  |  2KB  |  42 lines

  1. (*----------------------------------------------------------------------*)
  2. (*         Hex_To_Dec --- Convert hex string to decimal number          *)
  3. (*----------------------------------------------------------------------*)
  4.  
  5. FUNCTION Hex_To_Dec( S : AnyStr; Default : INTEGER ) : INTEGER;
  6.  
  7. (*----------------------------------------------------------------------*)
  8. (*                                                                      *)
  9. (*     Function:   Hex_To_Dec                                           *)
  10. (*                                                                      *)
  11. (*     Purpose:    Convert hex string to decimal number                 *)
  12. (*                                                                      *)
  13. (*     Calling Sequence:                                                *)
  14. (*                                                                      *)
  15. (*        Integ := Hex_To_Dec( S: AnyStr; Default: INTEGER ) : INTEGER; *)
  16. (*                                                                      *)
  17. (*           S       --- the hex string                                 *)
  18. (*           Default --- value to return if S not hex string            *)
  19. (*           Integ   --- corresponding decimal integer (0 if bad)       *)
  20. (*                                                                      *)
  21. (*----------------------------------------------------------------------*)
  22.  
  23. VAR
  24.    I  : INTEGER;
  25.    Sum: INTEGER;
  26.  
  27. BEGIN (* Hex_To_Dec *)
  28.  
  29.    Sum        := 0;
  30.    Hex_To_Dec := Default;
  31.  
  32.    FOR I := 1 TO LENGTH( S ) DO
  33.       CASE S[I] OF
  34.          '0'..'9':  Sum := Sum * 16 + ( ORD(S[I]) - ORD('0') );
  35.          'A'..'F':  Sum := Sum * 16 + ( ORD(S[I]) - ORD('A') + 10 );
  36.          ELSE       EXIT;
  37.       END;
  38.  
  39.    Hex_To_Dec := Sum;
  40.  
  41. END   (* Hex_To_Dec *);
  42.